package com.towa.ninemanmorris;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.AsyncTask;
import android.util.Log;

public class ReceivedTasker extends AsyncTask<String, Void, HighscoreWrapper> {

	@Override
	protected HighscoreWrapper doInBackground(String... params) {
		Socket socket = null;
		BufferedReader br;
		PrintWriter pw;
		HighscoreWrapper hsw = new HighscoreWrapper();
		try {
			socket = new Socket(NineManMorrisConstants.INET_ADDRESS, NineManMorrisConstants.INET_PORT);
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			pw = new PrintWriter(socket.getOutputStream());
			pw.println("getGroupHighscore::" + params[0] + "::" + 10);
			String hLength = br.readLine();
			String[] strings0 = hLength.split("::");
			int length = Integer.parseInt(strings0[1]);
			hsw.setGroupName(params[0]);
			for (int i = 0; i < length; i++) {
				MemberWrapper mw = new MemberWrapper();
				String temp = br.readLine();
				String[] strings1 = temp.split("::");
				mw.setUsername(strings1[0]);
				mw.setWin(Integer.parseInt(strings1[1]));
				mw.setLoss(Integer.parseInt(strings1[2]));
				hsw.getMembers().add(mw);
			}
		} catch (UnknownHostException e) {
			Log.e("ReceivedTasker", e.toString());
		} catch (IOException e) {
			Log.e("ReceivedTasker", e.toString());
		}
		return hsw;
	}

}
